package com.whut.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class EquipCheck {
    public static List<Integer> countRiskPoint(List<Map<String , Object>> list){
        int riskPoint1 = 0;
        int riskPoint2 = 0;
        int riskPoint3 = 0;
        int riskPoint4 = 0;
        for (int i = 0 ;i< list.size() ; i++){
            switch (Integer.parseInt(list.get(i).get("riskPoint").toString())){
                case 1:
                    riskPoint1++;
                    break;
                case 2:
                    riskPoint2++;
                    break;
                case 3:
                    riskPoint3++;
                    break;
                case 4:
                    riskPoint4++;
                    break;
                    default:
            }
        }
    //Map<String,Object> riskPointList = new HashMap<>();
        List<Integer> riskPointList = new ArrayList<>();
        riskPointList.add(list.size());
        riskPointList.add(riskPoint1);
        riskPointList.add(riskPoint2);
        riskPointList.add(riskPoint3);
        riskPointList.add(riskPoint4);

        return riskPointList;
    }
    //DivideCheckpartAndDes
    public static List<Map<String, Object>> divideCheckpartAndDes(List<Map<String, Object>> list){
        List<Map<String, Object>> problemList = list;
        List<Map<String, Object>> newProblemList = new ArrayList<>();
       // String[] allDividedCP;
        //List<String> allDividedCP= new ArrayList<>();
        for (int u = 0; u < problemList.size(); u++) {
            List<String> allDividedCP= new ArrayList<>();
            String riskPoint = problemList.get(u).get("riskPoint").toString();
            String cp = problemList.get(u).get("checkPart").toString();
            String[] tempCP = cp.split(",");
            String[] tempdividedCP=cp.split(",");
            for (int i = 0 ;i< tempCP.length ; i++){
                tempdividedCP = tempCP[i].split(";");
                for (int j = 0 ; j < tempdividedCP.length ; j ++){
                allDividedCP.add(tempdividedCP[j]);}
            }
//            String[] dividedCP = cp.split(",");
//            if (dividedCP.length ==1){
//                dividedCP = dividedCP[0].split(";");
//            }
            String des = problemList.get(u).get("problemDescription").toString();
            String[] dividedDes = des.split("；");
            String[] singleEquipName = allDividedCP.get(allDividedCP.size() - 1).split("#");
            //拼接数据
                for (int j = 0; j < allDividedCP.size(); j++) {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("checkPart", allDividedCP.get(j));
                    try {
                        map.put("problemDescription", dividedDes[j]);
                    } catch (Exception e) { map.put("problemDescription", dividedDes[0]);};
/*                    if (dividedDes.length > 1) map.put("problemDescription", dividedDes[j]);
                        //同样错误时不用分开
                    else map.put("problemDescription", dividedDes[0]);*/
                    map.put("riskPoint", riskPoint);
                    newProblemList.add(map);
                }
  /*              String[] singleEquipName = dividedCP[dividedCP.length - 1].split("#");
            //拼接数据
                for (int j = 0; j < dividedCP.length; j++) {156156
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("checkPart", dividedCP[j]);
                    if (dividedDes.length > 1) map.put("problemDescription", dividedDes[j]);
                        //同样错误时不用分开
                    else map.put("problemDescription", dividedDes[0]);
                    map.put("riskPoint", riskPoint);
                    newProblemList.add(map);
                }*/

        }
        return newProblemList;
    }

    //比较riskPoint
    public static void sortRiskPoint(List<Map<String, Object>> list) {
        Collections.sort(list, new ComparatRiskPoint());
    }


    //重写比较riskPoint
    static class ComparatRiskPoint implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Map map1 = JSONObject.parseObject(JSONObject.toJSONString(o1), Map.class);
            Map map2 = JSONObject.parseObject(JSONObject.toJSONString(o2), Map.class);
            int num1 = Integer.parseInt((String) map1.get("riskPoint"));
            int num2 = Integer.parseInt((String) map2.get("riskPoint"));
            if (num1 == num2) {
                return 0;
            } else if (num1 < num2) {
                return 1;
            }
            return -1;
        }
    }

    //比较checkPart首字母
    public static void sortCheckPart(List<Map<String, Object>> list) {
        Collections.sort(list, new ComparatCheckPart());
    }

    //重写比较checkPart首字母
    static class ComparatCheckPart implements Comparator {

        @Override
        public int compare(Object o1, Object o2) {
            Map map1 = JSONObject.parseObject(JSONObject.toJSONString(o1), Map.class);
            Map map2 = JSONObject.parseObject(JSONObject.toJSONString(o2), Map.class);
            String s1 = (String) map1.get("checkPart");
            String[] apart1 = s1.split("#");
            String s2 = (String) map2.get("checkPart");
            String[] apart2 = s2.split("#");
            int num1 = Integer.parseInt(apart1[0]);
            int num2 = Integer.parseInt(apart2[0]);
            if (num1 == num2) {
                return 0;
            } else if (num1 > num2) {
                return 1;
            }
            return -1;
        }
    }
}
